package com.pethome.pet.controller;

import com.pethome.common.annotation.Log;
import com.pethome.common.core.controller.BaseController;
import com.pethome.common.core.domain.AjaxResult;
import com.pethome.common.core.page.TableDataInfo;
import com.pethome.common.enums.BusinessType;
import com.pethome.common.utils.poi.ExcelUtil;
import com.pethome.pet.domain.Pet;
import com.pethome.pet.service.IPetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 宠物信息Controller
 * 
 * @author hjc
 * @date 2024-08-26
 */
@Slf4j
@RestController
@RequestMapping("/pet/pets")
public class PetController extends BaseController
{
    @Autowired
    private IPetService petService;






    /**
     * 宠物详情页  查询功能
     * @param pet
     * @return
     */
    @GetMapping("/getPetListByPet")
    public TableDataInfo getPetListByPet( Pet pet)
    {
        log.warn(" ==========search_pet"+pet.toString());
        pet.setPetStatus(2L);
        List<Pet> list = petService.selectPetList(pet);
        return getDataTable(list);
    }



    /**
     * 获取可以领养宠物信息    n条
     * @return n条宠物信息
     */
    @GetMapping("/getPetList")
    public TableDataInfo getPetList(){
        List<Pet> list = petService.getPetList();
        return getDataTable(list);
    }



    /**
     * 首页获取要展示的宠物信息
     * @return TableDataInfo
     */
    @GetMapping("/show")
    public TableDataInfo list(){
        List<Pet> list = petService.show();
        return getDataTable(list);
    }

    /**
     * 查看未被领养的宠物信息，
     * @param petId 宠物id
     * @return AjaxResult
     */
    @GetMapping(value = "/getPetByIdAndCanAdopt")
    public AjaxResult getPetByIdAndCanAdopt(@RequestParam("id")   Long petId)
    {
        log.info("Huoqu   可以领养的宠物信息");
       Pet pet =  petService.selectPetByPetIdAndStatus(petId);
       log.info(pet.toString());
       if(pet == null){
           return error("当前宠物信息不可查看");
       }
        return success(petService.selectPetByPetIdAndStatus(petId));
    }




    /**
     * 查询宠物信息列表
     */
    @PreAuthorize("@ss.hasPermi('pet:pets:list')")
    @GetMapping("/list")
    public TableDataInfo list(Pet pet)
    {
        startPage();
        List<Pet> list = petService.selectPetList(pet);
        return getDataTable(list);
    }

    /**
     * 导出宠物信息列表
     */
    @PreAuthorize("@ss.hasPermi('pet:pets:export')")
    @Log(title = "宠物信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Pet pet)
    {
        List<Pet> list = petService.selectPetList(pet);
        ExcelUtil<Pet> util = new ExcelUtil<Pet>(Pet.class);
        util.exportExcel(response, list, "宠物信息数据");
    }

    /**
     * 获取宠物信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('pet:pets:query')")
    @GetMapping(value = "/{petId}")
    public AjaxResult getInfo(@PathVariable("petId") Long petId)
    {
        return success(petService.selectPetByPetId(petId));
    }

    /**
     * 新增宠物信息
     */
    @PreAuthorize("@ss.hasPermi('pet:pets:add')")
    @Log(title = "宠物信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Pet pet)
    {
        return toAjax(petService.insertPet(pet));
    }

    /**
     * 修改宠物信息
     */
    @PreAuthorize("@ss.hasPermi('pet:pets:edit')")
    @Log(title = "宠物信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Pet pet)
    {
        return toAjax(petService.updatePet(pet));
    }

    /**
     * 删除宠物信息
     */
    @PreAuthorize("@ss.hasPermi('pet:pets:remove')")
    @Log(title = "宠物信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{petIds}")
    public AjaxResult remove(@PathVariable Long[] petIds)
    {
        return toAjax(petService.deletePetByPetIds(petIds));
    }
}
